/**
 * Copyright @ 2000 ysten Co. Ltd. 
 * All right reserved.     
 * @author: wangmin
 * date: 2016-07-11     
 */
package com.task.service.impl;

import com.task.bean.EnumConstants;
import com.task.entity.ChildTask;
import com.task.entity.FinishTasks;
import com.task.entity.User;
import com.task.repository.IChildTaskRepository;
import com.task.repository.IFinishTasksRepository;
import com.task.repository.IUserRepository;
import com.task.service.IFinishTasksService;
import com.task.service.IRedPackageService;
import com.task.utils.DownloadImageUtil;
import com.task.utils.PropertiesUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;

/**
 * 用户完成任务服务
 */
@Service
public class FinishTasksServiceImpl implements IFinishTasksService {

    @Autowired
    IFinishTasksRepository finishTasksRepository;

    @Autowired
    IChildTaskRepository childTaskRepository;

    @Autowired
    IUserRepository userRepository;

    @Autowired
    IRedPackageService redPackageService;

    private static final Logger LOGGER = LoggerFactory.getLogger(FinishTasksServiceImpl.class);


    @Override
    public void save(FinishTasks finishTasks) {
        finishTasksRepository.save(finishTasks);
    }

    @Override
    public FinishTasks findByChildTaskId(Long childTaskId) {
        return finishTasksRepository.findByChildTaskId(childTaskId);
    }

    @Override
    public Integer getCountByUserId(String userId) {
        return finishTasksRepository.getCountByUserId(userId);
    }

    @Override
    public void submitTask(Long childTaskId, String userId, String imageUrl, String accessToken, String taskDesc, String absolutePath) {
        EnumConstants.ChildTaskStatus childTaskStatus = EnumConstants.ChildTaskStatus.SUBMIT;
        ChildTask childTask = childTaskRepository.findOne(childTaskId);
        FinishTasks finishTask = new FinishTasks();
        finishTask.setChildTaskId(childTaskId);
        finishTask.setFinishTime(new Date());
        finishTask.setUserId(userId);
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isNotBlank(imageUrl)) {
            String[] images = imageUrl.split(",");
            String url = PropertiesUtil.getProperty("downImageUrl");
            for (String image : images) {
                String fileUrl = DownloadImageUtil.downloadMedia(accessToken, image, url, absolutePath);
                if (StringUtils.isNotBlank(fileUrl)) {
                    fileUrl = File.separator + fileUrl;
                    sb.append(fileUrl).append(",");
                }
            }
            LOGGER.info(sb.toString());
            if (sb.length() > 0) {
                finishTask.setImagesUrl(sb.substring(0, sb.length() - 1));
            }
        }
        finishTask.setTaskDesc(taskDesc);
        // 保存完成任务信息
        finishTasksRepository.save(finishTask);
        childTask.setFinishTime(new Date());
        childTask.setUserId(userId);
        childTask.setStatus(childTaskStatus);
        // 更新子任务状态
        childTaskRepository.save(childTask);
    }
}
